using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace LibGreen.Jabber.ProtocolElements.IqElements
{
    /// <summary>
    /// Defines an &lt;iq&gt; element with the &lt;bind&gt; subtype
    /// </summary>
    public static class IqBindElement
    {
        /// <summary>
        /// The xmlns of the IQ &lt;bind&gt; element
        /// </summary>
        private const string XMLNS = "urn:ietf:params:xml:ns:xmpp-bind";

        /// <summary>
        /// Creates a new &lt;iq&gt;&lt;bind /&gt;&lt;/iq&gt; stanza requesting a server-assigned resource
        /// </summary>
        /// <returns>A new &lt;iq&gt; element</returns>
        public static Element Create()
        {
            Element parent = IqElement.Create(IqType.Set);
            Dictionary<string, string> bindAttributes = new Dictionary<string, string>(2);
            bindAttributes["xmlns"] = XMLNS;
            parent.Children.Add(Element.CreateElement("bind", bindAttributes));
            return parent;
        }

        /// <summary>
        /// Creates a new &lt;iq&gt;&lt;bind /&gt;&lt;/iq&gt; stanza requesting a specific resource
        /// </summary>
        /// <param name="resource">The resource to which to bind</param>
        /// <returns>A new &lt;iq&gt; element</returns>
        public static Element Create(string resource)
        {
            Element parent = IqElement.Create(IqType.Set);

            Dictionary<string, string> bindAttributes = new Dictionary<string, string>(2);
            bindAttributes["xmlns"] = XMLNS;
            parent.Children.Add(Element.CreateElement("bind", bindAttributes));

            // Add the <resource> tag to the <bind> element
            parent.Children[0].Children.Add(Element.CreateElement("resource", new Dictionary<string, string>(), resource));
            return parent;
        }
    }
}
